package com.ihomey.linkuphome.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ihomey.linkuphome.R;
import com.ihomey.linkuphome.entities.Device;
import com.ihomey.linkuphome.listeners.OnItemClickListener;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuAdapter;

import java.util.List;

/**
 * Created by Administrator on 2016/12/28.
 */

public class ControlDeviceListAdapter extends SwipeMenuAdapter<ControlDeviceListAdapter.ListHolder> {
    private List<Device> mDatas;
    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public ControlDeviceListAdapter(List<Device> list) {
        this.mDatas = list;
    }

    public Device getItem(int position) {
        return mDatas.get(position);
    }

    public void replaceAll(List<Device> list) {
        mDatas.clear();
        mDatas.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public void onBindViewHolder(ListHolder holder, final int position) {
        if(position!=getItemCount()-1){
            Device controlUnit = mDatas.get(position);
            holder.nameText.setText(controlUnit.getName());
        }else{
            holder.nameText.setText(R.string.initialize);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(position);
                }
            }
        });
        return;
    }

    @Override
    public View onCreateContentView(ViewGroup parent, int viewType) {
        View layout = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_control_unit, parent, false);
        return layout;
    }

    @Override
    public ListHolder onCompatCreateViewHolder(View realContentView, int viewType) {
        return new ListHolder(realContentView);
    }

    class ListHolder extends RecyclerView.ViewHolder {
        TextView nameText;

        public ListHolder(View itemView) {
            super(itemView);
            nameText = (TextView) itemView.findViewById(R.id.tv_control_unit_name);
        }
    }

    @Override
    public int getItemCount() {
        return mDatas.size()+1;
    }
}
